//
//  BSActivityDatailsViewController.m
//  BS
//
//  Created by 王浩宇 on 15/8/21.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BSActivityDatailsViewController.h"
#import "BSActivityDatalisTableViewCell.h"
#import "Header.h"
#import "LJRequestManagerBlock.h"
#import "BSActivityDatalistInfoModel.h"

#import "BSHotTableVC.h"

#import "BSListModel.h"

#import "BSTextCell.h"

#import "BSHeaderView.h"

#import "BSFooterView.h"

#import "BSVideoCell.h"

#import "BSImageCell.h"

#import "PlayerManger.h"

#import "SDImageCache.h"

#import "MJRefresh.h"

#import "BSDataBaseManager.h"

#import "BSVoiceCell.h"
#import "BSActivityViewController.h"

@interface BSActivityDatailsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *infoArray;
@property (nonatomic, strong)NSMutableArray *datalistArray;

@property (nonatomic, strong)NSMutableDictionary *typDic;

@end

@implementation BSActivityDatailsViewController
- (NSMutableArray *)infoArray
{
    if (_infoArray == nil) {
        _infoArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _infoArray;
}
- (NSMutableArray *)datalistArray
{
    if (_datalistArray == nil) {
        _datalistArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _datalistArray;
}
- (void)findData
{
    NSString *str = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?a=theme_info&appname=baisishequ&asid=9A2EA4AE-2DEB-4479-A977-8CB5B3EFF60F&c=topic&theme_id=%@",self.themeID];
    [LJRequestManagerBlock manager:GET requestWith:str dic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *infoDic = dict[@"info"];
            BSActivityDatalistInfoModel *model = [[BSActivityDatalistInfoModel alloc] init];
            [model setValuesForKeysWithDictionary:infoDic];
            [self.infoArray addObject:model];
        
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
    NSString *dataStr = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?a=theme_datalist&appname=baisishequ&asid=9A2EA4AE-2DEB-4479-A977-8CB5B3EFF60F&c=topic&client=iphone&jbk=0&mac=&market=&openudid=f60e4fa5905641dfd3bec86efbf33da57105acc6&order=new&page=1&pagesize=20&sex=m&theme_id=%@&type=0",self.themeID];
    [LJRequestManagerBlock manager:GET requestWith:dataStr dic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = dict[@"list"];
        for (NSDictionary *listDic in arr) {
            BSListModel *model = [[BSListModel alloc] init];
            [model setValuesForKeysWithDictionary:listDic];
            [self.datalistArray addObject:model];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
    
}

- (instancetype)init{

    if (self = [super init]) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self findData];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creationTableView];
    self.navigationItem.title = self.themeName;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(doBack)];
    // Do any additional setup after loading the view.
}
- (void)doBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creationTableView
{
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"BSActivityDatalisTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BSVideoCell" bundle:nil] forCellReuseIdentifier:@"videoCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BSTextCell" bundle:nil] forCellReuseIdentifier:@"textCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BSImageCell" bundle:nil] forCellReuseIdentifier:@"imageCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BSVoiceCell" bundle:nil] forCellReuseIdentifier:@"voiceCell"];
    [self.tableView registerClass:[BSHeaderView class] forHeaderFooterViewReuseIdentifier:@"header"];
    [self.tableView registerClass:[BSFooterView class] forHeaderFooterViewReuseIdentifier:@"footer"];
    
    
    
    [self.view addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _datalistArray.count + _infoArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        BSActivityDatalisTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        BSActivityDatalistInfoModel *infoModel = _infoArray[indexPath.section];
        cell.model = infoModel;
        return cell;
    }
        BSListModel *model = self.datalistArray[indexPath.section - 1];
        if ([model.type isEqualToString:@"29"]) {
            BSTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
            textCell.model = model;
            return textCell;
        }
        if ([model.type isEqualToString:@"10"]) {
            BSImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier:@"imageCell" forIndexPath:indexPath];
            imageCell.vc = self;
            imageCell.model = model;
            return imageCell;
        }
        if ([model.type isEqualToString:@"41"]) {
            BSVideoCell *videoCell = [tableView dequeueReusableCellWithIdentifier:@"videoCell" forIndexPath:indexPath];
            videoCell.model = model;
            videoCell.vc = self;
            return videoCell;
        }
        if ([model.type isEqualToString:@"31"]) {
            BSVoiceCell *voiceCell = [tableView dequeueReusableCellWithIdentifier:@"voiceCell" forIndexPath:indexPath];
            voiceCell.model = model;
            voiceCell.vc = self;
            return voiceCell;
        }
        
        BSTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
        return textCell;
    
}




- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }
    else{
        BSHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
        view.model = self.datalistArray[section - 1];
        return view;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }else{
    return 50;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }else{
        BSFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footer"];
        view.model = self.datalistArray[section - 1];
        view.nav = self.navigationController;
        return view;

    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }else{
    return 50;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        BSActivityDatalistInfoModel *model = _infoArray[indexPath.row];
        return [BSActivityDatalisTableViewCell cellforHight:model];

    }
        BSListModel *model = self.datalistArray[indexPath.section - 1];
    if ([model.type isEqualToString:@"29"]) {
        return [BSTextCell heightOfCell:model];
    }
    if ([model.type isEqualToString:@"10"]) {
        return [BSImageCell heightOfCell:model];
    }
    if ([model.type isEqualToString:@"41"]) {
        return [BSVideoCell heightOfCell:model];
    }
    if ([model.type isEqualToString:@"31"]) {
        return [BSVoiceCell heightOfCell:model];
    }
    float ecg;
    if(isnan(ecg)){
        
        ecg = 0.0;
    }
    else {
        
    }
    return 1;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
